// TODO: CODE REVIEW & CLEANUP!
//
// GenAv.xaml.cs
//

using System;
using System.Windows.Controls;
using HomeUX.Controls;
using HomeUX.ClientControlSupport;
using HomeUX.Utilities;
using HomeUX.UiUtilities;
using HomeUX.Synchronization;
using HomeUX.MusicClientLibrary;
using System.Windows.Media;
using HomeUX.ClientAppSupport;

namespace HomeUX.ClientApp
{

public partial class GenAudio : UserControl
{
    Control _dialog;
    SyncNode _laundryRoomAudioSourceNode;
    SyncNode _deckAudioSourceNode;
    SyncNode _patioAudioSourceNode;

    public GenAudio()
    {
        InitializeComponent();
        ISyncClient syncClient = ClientControlSupport.ClientGlobals.GetSyncClient(this);

        _laundryRoomAudioSourceNode = (SyncNode) syncClient.SyncTree.CreateNode(SyncPath.Build("Laundry Room Audio", "Source"));
        _laundryRoomAudioSourceNode.ValueChanged += _laundryRoomAudioSourceNode_ValueChanged;
        _laundryRoomAudioSourceNode_ValueChanged(null, null);

        _deckAudioSourceNode = (SyncNode) syncClient.SyncTree.CreateNode(SyncPath.Build("Deck Audio", "Source"));
        _deckAudioSourceNode.ValueChanged += _deckAudioSourceNode_ValueChanged;
        _deckAudioSourceNode_ValueChanged(null, null);

        _patioAudioSourceNode = (SyncNode) syncClient.SyncTree.CreateNode(SyncPath.Build("Patio Audio", "Source"));
        _patioAudioSourceNode.ValueChanged += _patioAudioSourceNode_ValueChanged;
        _patioAudioSourceNode_ValueChanged(null, null);
    }

    void _laundryRoomAudioSourceNode_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        LaundryRoom_ControlMediaButton.IsEnabled = (_laundryRoomAudioSourceNode.Value != null);
    }

    void _deckAudioSourceNode_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        Deck_ControlMediaButton.IsEnabled = (_deckAudioSourceNode.Value != null);
    }

    void _patioAudioSourceNode_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        Patio_ControlMediaButton.IsEnabled = (_patioAudioSourceNode.Value != null);
    }

    private void LaundryRoom_ControlMediaButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        ControlMedia(LaundryRoom_Source);
    }

    private void Deck_ControlMediaButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        ControlMedia(Deck_Source);
    }

    private void Patio_ControlMediaButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        ControlMedia(Patio_Source);
    }

    void ControlMedia(TouchDropDownList sourceDropDownList)
    {
        switch (sourceDropDownList.SelectedItemValue.ToString())
        {

        case "TiVo":

            _dialog = new McTivo();
			break;

        case "Music":

            var musicController = new MusicController()
            {
                Name = "GenAudioMusicController",
                IsNowPlayingMode = true,
                SyncPath = "Music Server"
            };
            musicController.BrowseArtistsButtonClick += delegate
            {
                ClientFramework.Current.GoToScreen("Music Server Library", "GeneralScreen");
            };
            musicController.SearchButtonClick += delegate
            {
                ClientFramework.Current.GoToScreen("Music Server Search", "GeneralScreen");
            };
            musicController.ViewThisArtistButtonClick += delegate
            {
                string artistName = musicController.CurrentArtistName;
                if (!String.IsNullOrEmpty(artistName))
                {
                    Screen screen;
                    MusicArtistScreen screenControl;
                    ClientFramework.Current.FindScreen("Music Server Artist", out screen, out screenControl);
                    screenControl.ArtistName = artistName;
                    ClientFramework.Current.GoToScreen(screen.Name, "GeneralScreen");
                }
            };
            musicController.ViewThisAlbumButtonClick += delegate
            {
                string albumId = musicController.CurrentAlbumId;
                if (!String.IsNullOrEmpty(albumId))
                {
                    Screen screen;
                    MusicAlbumScreen screenControl;
                    ClientFramework.Current.FindScreen("Music Server Album", out screen, out screenControl);
                    screenControl.AlbumId = albumId;
                    screenControl.HighlightTrackNumber = -1;
                    ClientFramework.Current.GoToScreen(screen.Name, "GeneralScreen");
                }
            };
            musicController.ViewQueueButtonClick += delegate
            {
                ClientFramework.Current.GoToScreen("Music Server Queue", "GeneralScreen");
            };
            _dialog = musicController;
			break;

        case "DVD/CD":

			_dialog = new McDvd();
			break;

        case "VCR":

			_dialog = new McVcr();
			break;

        default:

			throw new InvalidOperationException("Internal error");
        }
        var settings = new DialogSettings()
        {
            BackgroundMargin = 8,
            OnClose = delegate
            {
                UiUtil.DisposeVisualTree(_dialog);
            }
        };
        ClientUtil.ShowDialog(this, _dialog, settings);
    }
}

}
